'''
Created on 24/06/2014

@author: H
'''
import os
import sys



class SessionManager:
    '''
    classdocs
    
    Simple Sequenced Session Manager (3SM)
    '''
    

    def iniciar(self):
        '''
        Constructor
       
        self.session ={"Input path","Output path",
                       "Input buffer", "Output buffer",
                        "Listdir", "Current file number",
                         "Current in"}
        '''
        
    def save(self, IntP,OutP,InB,OutB,Listdir,CFN,CIn):
        self.session = {}
        self.session["Input path"]= IntP
        self.session["Output path"]=  OutP  
        self.session["Input buffer"]=  InB
        self.session["Output buffer"]=  OutB  
        self.session["Listdir"]=  Listdir  
        self.session["Current file number"]=  CFN  
        self.session["Current in"]=  CIn  
        
        sessionPath = os.path.dirname(os.path.abspath(__file__))
        sessionFile = sessionPath+os.path.sep+"session.sss"
        with open(sessionFile,"w") as fout:
            fout.write(self.session["Input path"].__str__()+"\n")
            fout.write(self.session["Output path"].__str__()+"\n")
            fout.write(self.session["Input buffer"].__str__()+"\n")
            fout.write(self.session["Output buffer"].__str__()+"\n")
            fout.write(self.session["Listdir"].__str__()+"\n")
            fout.write(self.session["Current file number"].__str__()+"\n")
            fout.write(self.session["Current in"].__str__()+"\n")
            
            
    def load(self, sessionfile):
        if sessionfile == "":
            sessionfile = os.path.dirname(os.path.abspath(__file__))+os.path.sep+"session.sss"
            
        if not os.path.isfile(sessionfile):
            print "Invalid session file, exiting..."
            sys.exit()
        self.session = {}
        with open(sessionfile,"r") as fin:
            self.session["Input path"]= fin.readline().__str__().rstrip()
            self.session["Output path"]=  fin.readline().__str__().rstrip()
            self.session["Input buffer"]=  int(fin.readline().rstrip())
            self.session["Output buffer"]=  int(fin.readline().rstrip())
            self.session["Listdir"]=  fin.readline().__str__().rstrip()
            self.session["Current file number"]=  int(fin.readline().rstrip())
            self.session["Current in"]=  int(fin.readline().rstrip())
        return self.session
       
        